<?php

namespace App\Http\Controllers\V1\Admin;


use App\Models\V1\Agent;
use App\Models\V1\Community;
use Illuminate\Http\Request;
use App\Http\Requests\Admin\CommunityServer;

class CommunityController extends BaseController
{

    /**
     * 小区列表
     * @param CommunityServer $request
     * @return \Illuminate\Http\Response
     */
    public function index(CommunityServer $request)
    {

        $user = auth('box_admin')->user();

        $community_id =$user['community_id'];




        $query = Community::query();
        $query->where('id', $community_id);
        $pageSize = $request->input('pageSize', 10);
        $order = $request->input('order', 'desc');
        $sort = $request->input('sort', 'id');
        if ($request->filled('name')) {
            $query->where('name', 'like', "%" . $request->input('name', "") . "%");
        }
        if ($request->filled('province_id')) {
            $query->where('province_id', $request->input('province_id'));
        }
        if ($request->filled('city_id')) {
            $query->where('city_id', $request->input('city_id'));
        }
        if ($request->filled('district_id')) {
            $query->where('district_id', $request->input('district_id'));
        }

        if ($request->filled('agent_id')) {
            $query->where('agent_id', $request->input('agent_id'));
        }

        $query->orderBy($sort, $order);
        $data = $query->paginate($pageSize);
        return ajax_output(200, "成功", $data);
    }


    /**
     * 增加小区
     *
     * @param CommunityServer $request
     * @return \Illuminate\Http\Response
     */
    public function store(CommunityServer $request)
    {

        $request->validate('v1.create');
        $model = new Community();
        $agent_id = $request->input('agent_id','');
        if($agent_id){
            $agent_name = Agent::where('id', $agent_id)->value('username');
        }else{
            $agent_name = '';
        }
        $model->create([
            'name' => $request->input('name'),
            'thumb' => $request->input('thumb', ""),
            'construct_time' => $request->input('construct_time', ""),
            'occy_time' => $request->input('occy_time', ""),
            'acreage' => $request->input('acreage'),
            'household_num' => $request->input('household_num'),
            'shop_num' => $request->input('shop_num'),
            'garage_num' => $request->input('garage_num'),
            'introductory' => $request->input('introductory'),
            'province_id' => $request->input('province_id'),
            'province_name' => $request->input('province_name'),
            'city_id' => $request->input('city_id'),
            'city_name' => $request->input('city_name'),
            'district_id' => $request->input('district_id'),
            'district_name' => $request->input('district_name'),
            'address' => $request->input('address'),
            'state' => $request->input('state', 1),
            'partition' => $request->input('partition'),
            'building' => $request->input('building'),
            'floor' => $request->input('floor'),
            'hnum' => $request->input('hnum'),
            'agent_id' => $agent_id,
            'agent_name' => $agent_name,
            'element' => $request->input('element'),
        ]);
        return ajax_output(200, "成功");
    }

    /**
     * 小区详情
     *
     * @param int $id
     * @return \Illuminate\Http\Response
     */
    public function show($id)
    {
        $vo = Community::find($id);
        return ajax_output(200, "成功", $vo);
    }


    /**
     * 更新小区
     *
     * @param \Illuminate\Http\Request $request
     * @param int $id
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request, $id)
    {

        $vo = Community::find($id);
        $agent_id = $request->input('agent_id','');
        if($agent_id){
            $agent_name = Agent::where('id', $agent_id)->value('username');
        }else{
            $agent_name = '';
        }

        $vo->update([
            'name' => $request->input('name'),
            'thumb' => $request->input('thumb', ""),
            'construct_time' => $request->input('construct_time', ""),
            'occy_time' => $request->input('occy_time', ""),
            'acreage' => $request->input('acreage'),
            'household_num' => $request->input('household_num'),
            'shop_num' => $request->input('shop_num'),
            'garage_num' => $request->input('garage_num'),
            'introductory' => $request->input('introductory'),
            'province_id' => $request->input('province_id'),
            'province_name' => $request->input('province_name'),
            'city_id' => $request->input('city_id'),
            'city_name' => $request->input('city_name'),
            'district_id' => $request->input('district_id'),
            'district_name' => $request->input('district_name'),
            'address' => $request->input('address'),
            'state' => $request->input('state', 1),
            'partition' => $request->input('partition'),
            'building' => $request->input('building'),
            'floor' => $request->input('floor'),
            'hnum' => $request->input('hnum'),
            'agent_id' => $agent_id,
            'agent_name' => $agent_name,
            'element' => $request->input('element'),
        ]);

        return ajax_output(200, "成功");
    }

    /**
     * 删除广告
     *
     * @param int $id
     * @return \Illuminate\Http\Response
     */
    public function destroy($id)
    {
        Community::destroy($id);
        return ajax_output(200, "成功");
    }
}
